﻿using KAKA.Domain;
using KAKA.Repository;
using Morus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KAKA
{
    public class MobileVerificationAttribute : FilterAttribute, IAuthorizationFilter
    {
        public MobileVerificationAttribute()
        {
            Order = 4;
        }

        public void OnAuthorization(AuthorizationContext filterContext)
        {
            var httpContext = filterContext.HttpContext;

            if (httpContext.User.Identity.IsAuthenticated)
            {
                var user = WebUtil.GetCurrentUser();

                var member = IoC.Resolve<IMemberRepository>().Get(user.Id);

                if (member.MobileVerified == false)
                {
                    HandleMobileVerifyFailedRequest(filterContext);
                }
            }
        }

        protected virtual void HandleMobileVerifyFailedRequest(AuthorizationContext filterContext)
        {
            filterContext.Result = new MobileUnverifiedResult();
        }
    }
}